/*
 * @# uploadPhotos.js 2011-12-28 下午03:19:12
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */

var UploadPhotosDialog = {
	_haveInited : false,
	_$dialog : $('#dialog-upload-photos'),
	_createType : false,
	_createCallback : false,
	_files : {},
	
	_init : function() {
		this._haveInited = true;
		
		var self = this;
		this._$dialog.dialog({
			modal : true,
			title : '上传相片',
			autoOpen : false,
			width : 950,
			height : 500,
			resizable : false
		});
		this._bindEvents();
	},
	
	_bindEvents : function() {
		var self = this;
		
		$('#dialog-upload-albums-btn').click(function(e) {
			EdibAlbumDialog.createShows(function(album) {
				$('#dialog-upload-albums-list').prepend('<option value="' + album.id + '" selected>' + album.name + '</option>');
			});
		});
		var uploadFileElement = document.getElementById("diaog-upload-file");  
		uploadFileElement.addEventListener("change", this._handleFileChange, false);

		var dropbox = document.getElementById('dialog-upload-box');
		dropbox.addEventListener('dragover', function(e) { 
			e.stopPropagation();							 
			e.preventDefault();								 
		}, false);
		dropbox.addEventListener('drop', this._handleDrop, false);
		
		$("#dialog-upload-datas li").hover(function(e) {
			if ($(this).is(':has(img)')) {
				$('.delPic', this).show();
			}
		}, function(e) {
			$('.delPic', this).hide();
		});
		$("#dialog-upload-datas li .delThis").click(function(){
			var img = $(this).parent().siblings('img'),
				fileKey = $(img).attr('fileKey');
			delete self._files[fileKey];
			img.remove();
		});
		
		$('#dialog-upload-photos-submit').click(function(e) {
			for (var fileKey in self._files) {
				console.log(self._files[fileKey]);
			}
		});
		$('#dialog-upload-photos-cancel').click(function(e) {
			self.close();
		});
		$('#dialog-upload-photos-local,#dialog-upload-photos-continue').click(function(e) {
			uploadFileElement.click();
			return false;
		});
	},
	_handleDrop : function(e) {
		e.stopPropagation();
		e.preventDefault();
		
		UploadPhotosDialog._handleFiles(e.dataTransfer.files);
	},
	
	_handleFiles : function(files) {
		var self = this;
		
		var $divimg = $("#dialog-upload-datas li:not(:has(img))");
		if(files || files.length > 0){
			$('#dailog-upload-tips').hide();
			$('#dialog-upload-datas').show();
			$('#dialog-upload-photos-continue').show();
		};
		if ($.browser.mozilla || $.browser.webkit) {
			$.each(files, function(index, file) {
				if (!file.type.match(/^image\//)) {  //检测文件是否图片
					return true;
				}
				var fileKey = self._fileKey(file);
				if (self._files[fileKey]) { // 已加载
					return true;
				} else {
					self._files[fileKey] = file;
				}
				var imgUrl = '';
				if ($.browser.mozilla) { //FF4+
					imgUrl = window.URL.createObjectURL(this);
				} else { // Chrome8+
					imgUrl = window.webkitURL.createObjectURL(this);
				}
				$divimg.eq(index).append('<img src=' + imgUrl + ' alt="" fileKey="' + fileKey + '" />');
			});
		　}
	},
	
	_fileKey : function(file) {
		return 'n:' + file.fileName + '/s:' + file.size;
	},
	
	_handleFileChange : function(e) {
		UploadPhotosDialog._handleFiles(this.files);
	},
	
	createShows : function(album, callback) {
		this._create('shows', album, callback);
	},
	createDress : function(album, callback) {
		this._create('dress', album, callback);
	},
	
	_create : function(type, album, callback) {
		if ($.isFunction(album)) {
			callback = album;
			album = null;
		}
		this._createType = type;
		this._createCallback = callback;
		
		if (null == album) {
			$.getJSON(window.ctx + '/albums/list', function(albums) {
				var options = '';
				$.each(albums, function(index, album) {
					options += '<option value="' + album.id + '">' + album.name + '</option>';
				});
				$('#dialog-upload-albums-list').html(options);
			});	
			
			this._open();
		} else {
			$('#dialog-upload-albums-list-wrapper').hide();
			$('#dialog-upload-albums-list').html('<option value="' + album.id + '">' + album.name + '</option>');
		
			this._open(album.name);
		}
	},
	
	_open : function(title) {
		if (!this._haveInited) {
			this._init();
		}
		this._$dialog.dialog('open').dialog("option", "title", title || '上传相片');
	},
	
	close : function() {
		this._$dialog.dialog('close');
	}

};